package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.AddressService;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @Reference
    private SetmealService setmealService;

    @Reference
    private AddressService addressService;

    /**
     * 1.分页查询
     *
     * @param pageBean
     * @return
     */
    @PostMapping("/pagination")
    public PageResult pagination(@RequestBody QueryPageBean pageBean) {
        PageResult result = null;
        try {
            result = orderService.getPage(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 2.后台添加预约,拉取套餐信息,门店信息
     */
    @GetMapping("/list")
    public Result list() {
        HashMap<String, Object> result = null;

        try {
            result = new HashMap<>();

            List<Setmeal> setMeals = setmealService.getSetmealAll();
            List<Address> stores = addressService.getAllAddress();

            result.put("stores", stores);
            result.put("setMeals", setMeals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
        return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, result);
    }

    /**
     * 3.创建预约记录操作
     *
     * @param setMealIds
     * @param map
     * @return
     */
    @PostMapping("/create/{setMealIds}")
    public Result create(@PathVariable Integer[] setMealIds, @RequestBody Map map) {
        Result result = null;
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            result = orderService.create(setMealIds, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable Integer id) {
        try {
            orderService.updateById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ORDER_SUCCESS);
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            orderService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CANCEL_ORDER_FAIL);
        }
        return new Result(true, MessageConstant.CANCEL_ORDER_SUCCESS);
    }

    @GetMapping("/findByMemberId/{id}")
    public Result findByMemberId(@PathVariable Integer id) {
        try {
            List<Map<String,Object>> list = orderService.findByMemberId(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
